﻿using System.Collections.Generic;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Utilities.Tests
{
    [TestClass()]
    public class TemplateTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod()]
        public void TemplateApplyTextNoPlaceholdersTest()
        {
            Template template = new Template
            {
                //StartDelimiter = '$', Default
                //EndDelimiter = '$', Default
                Text = "Some text"
            };

            Assert.AreEqual("Some text", template.Apply(null));
        }

        [TestMethod()]
        public void TemplateApplyTextEscapeDelimiterTest()
        {
            Template template = new Template
            {
                //StartDelimiter = '$', Default
                //EndDelimiter = '$', Default
                Text = "$$"
            };

            Assert.AreEqual("$", template.Apply(null));
        }

        [TestMethod()]
        public void TemplateApplyTextEscapeStartDelimiterTest()
        {
            Template template = new Template
            {
                StartDelimiter = '{',
                EndDelimiter = '}',
                Text = "{{"
            };

            Assert.AreEqual("{", template.Apply(null));
        }

        [TestMethod()]
        public void TemplateApplyTextEscapeEndDelimiterTest()
        {
            Template template = new Template
            {
                StartDelimiter = '{',
                EndDelimiter = '}',
                Text = "}}"
            };

            Assert.AreEqual("}", template.Apply(null));
        }

        [TestMethod()]
        public void TemplateApplyTextEscapeStartAndEndDelimiterTest()
        {
            Template template = new Template
            {
                StartDelimiter = '{',
                EndDelimiter = '}',
                Text = "{{}}"
            };

            Assert.AreEqual("{}", template.Apply(null));
        }

        [TestMethod()]
        public void TemplateApplyTextEscapeDelimiterAndMoreTextTest()
        {
            Template template = new Template
            {
                //StartDelimiter = '$', Default
                //EndDelimiter = '$', Default
                Text = "Although I don't like $$, I need lots of $$$$$$ to develop high quality software!"
            };

            Assert.AreEqual("Although I don't like $, I need lots of $$$ to develop high quality software!", template.Apply(null));
        }

        [TestMethod()]
        public void TemplateApplyEmptyTextNoValueOrWrongNameTest()
        {
            Template template = new Template
            {
                //StartDelimiter = '$', Default
                //EndDelimiter = '$', Default
                Text = "$v$"
            };

            Assert.AreEqual("[v]", template.Apply(null));

            Assert.AreEqual("[v]", template.Apply(
                new Dictionary<string, object>
                {
                    { "val", "value2" } // Wrong name val
                }));
        }

        [TestMethod()]
        public void TemplateApplyEmptyTextOneValueOnlyTest()
        {
            Template template = new Template
            {
                //StartDelimiter = '$', Default
                //EndDelimiter = '$', Default
                Text = "$v$"
            };

            Assert.AreEqual("value1", template.Apply(
                new Dictionary<string, object>
                {
                    { "v", "value1" }
                }));

            Assert.AreEqual("value2", template.Apply(
                new Dictionary<string, object>
                {
                    { "v", "value2" }
                }));
        }

        [TestMethod()]
        public void TemplateApplyEmptyTextMultipleValuesTest()
        {
            Template template = new Template
            {
                //StartDelimiter = '$', Default
                //EndDelimiter = '$', Default
                Text = "B $1$$2$.$3$ E"
            };

            Assert.AreEqual("B value1value2.value3 E", template.Apply(
                new Dictionary<string, object>
                {
                    { "1", "value1" },
                    { "2", "value2" },
                    { "3", "value3" },
                }));

            Assert.AreEqual("B value1[2].[3] E", template.Apply(
                new Dictionary<string, object>
                {
                    { "1", "value1" }
                }));

            Assert.AreEqual("B [1]value2.[3] E", template.Apply(
                new Dictionary<string, object>
                {
                    { "2", "value2" }
                }));
        }
    }
}
